package com.ddzh.mq;

import com.ddzh.cst.SysConst;
import com.ddzh.entity.Result;
import com.ddzh.mapper.MemberMapper;
import com.ddzh.utils.RedisUtil;
import com.ddzh.utils.SMSUtil;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * mq消费方
 */
@Component //spring ioc容器环境下，需要将对象置于spring的管理下
public class SMSValifyCodeConsumer {

//    @Resource
//    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 消费消息方
     * @param telephone 消费的消息内容
     */
    @RabbitListener(queues = "sms_queue")
    public void reciver(String telephone){
        //如何处理消息？？
        //获取短信验证码
        String code = SMSUtil.sendSmsCode(telephone);
        //将短信验证码存入redis中 - tel:code
//        redisTemplate.opsForValue().set(SysConst.SMS_VALIFYCODE+telephone,code,1, TimeUnit.HOURS);
        RedisUtil.set(SysConst.SMS_VALIFYCODE+telephone,code,1, TimeUnit.HOURS);
    }
    /**
     * @Description:  使用RedisTemplate键出现乱码（Can not load key value.Key was removed or redis-server went away.）加上下面代码
     * @author: panboyang
     * @date :2019-07-02 17:30:57
     * @params: No such property: code for class: Script1
     */
//    @Bean
//    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
//        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(redisConnectionFactory);
//        // 使用Jackson2JsonRedisSerialize 替换默认序列化
//        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//
//        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
//
//        // 设置value的序列化规则和 key的序列化规则
//        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//        redisTemplate.setKeySerializer(new StringRedisSerializer());
//
//        redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
//        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
//
//        redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
//        redisTemplate.setEnableDefaultSerializer(true);
//        redisTemplate.afterPropertiesSet();
//        return redisTemplate;
//    }
}
